package com.dlc.felear.xiaoensale.utils;

import android.util.Log;

import com.dlc.felear.xiaoensale.helper.JsonHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HandlerTemperatorUtil {
    private static final String TAG = "HandlerTemperatorUtil";
    private static HandlerTemperatorUtil instance;
    private HandlerTemperatorUtilCallback callback;
    private List<Integer> tempList = new ArrayList<>(256);
    private int recieveFlag = 0;
    private int tempMin, tempmax, tempVag;
    private int dataSize = 256;
    private float s=90f/77f;

    public void setCallback(HandlerTemperatorUtilCallback callback) {
        this.callback = callback;
    }

    private HandlerTemperatorUtil() {
    }

    public static HandlerTemperatorUtil getInstance() {
        if (instance == null) {
            synchronized (HandlerTemperatorUtil.class) {
                if (instance == null) {
                    instance = new HandlerTemperatorUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 解析接受到的红外数据
     *
     * @param data
     */
    public void recievetempertor(byte[] data) {
        log("data=" + HexUtil.bytes2HexString(data) + " recieveFlag=" + recieveFlag+" size="+data.length);
        try {
            if (data.length != 64) {
                log("数据长度不符");
                return;
            }
            if (recieveFlag == 0) {
                tempList.clear();
                //第一帧数据 需要去head数据
                for (int i = 15; i < data.length - 1; i += 2) {
                    long temp1 = (HexUtil.hex2decimal(HexUtil.encodeHexStr(Arrays.copyOfRange(data, i, i + 2))) - 27315) / 100;
                    tempList.add((int) (temp1));
                }
            } else if (recieveFlag > 0 && recieveFlag <= 8) {
                for (int i = 5; i < data.length - 1; i += 2) {
                    long temp1 = (HexUtil.hex2decimal(HexUtil.encodeHexStr(Arrays.copyOfRange(data, i, i + 2))) - 27315) / 100;
                    tempList.add((int) temp1);
                }
            }
            recieveFlag++;
            if ((byte) 0xAA == data[0] && (byte) 0x1D == data[1]) {
                handlevalue();
                recieveFlag = 0;
                if (callback != null && tempList.size() == 256) {
                    log("tempmax="+tempmax+" tempVag="+tempVag+" tempMin="+tempMin);
                    callback.tempCallback(tempmax, tempVag, tempMin, tempList);
                }
            }
        } catch (Exception e) {
            log(e.getMessage());
        }
    }

    /**
     * 处理数据
     */
    private void handlevalue() {
        if (tempList.size() != dataSize) {
            return;
        }
        tempMin = tempList.get(0);
        tempmax = tempList.get(0);
        tempVag = tempList.get(0);
        int temp1 = 0;
        for (Integer temp : tempList) {
            if (tempMin > temp) {
                tempMin = temp;
            }
            if (tempmax < temp) {
                tempmax = temp;
            }
            temp1 += temp;
        }
        int count = 0;
        int temp2 = 0;
        tempVag=temp1 / dataSize;
        for (Integer temp : tempList) {
            if (temp > tempMin) {
                count++;
                temp2 += temp;
            }
        }
        if (count != 0) {
            tempVag = (int) (temp2 / count);
        }
        log("补偿前 最低温度:" + tempMin + " 最高温度:" + tempmax + " 平均温度:" + tempVag+" 补偿系数:"+s);
        tempMin=(int) (tempMin*s);
        tempmax=(int) (tempmax*s);
        tempVag=(int) (tempVag*s);
        log("补偿后 最低温度:" + tempMin + " 最高温度:" + tempmax + " 平均温度:" + tempVag);
    }

    /**
     * 获取最大值
     *
     * @return
     */
    public int getTempmax() {
        return tempmax;
    }

    /**
     * 获取平均值
     *
     * @return
     */
    public int getTempVag() {
        return tempVag;
    }

    /**
     * 获取最小值
     *
     * @return
     */
    public int getTempMin() {
        return tempMin;
    }

    private void log(String msg) {
        Log.e(TAG, msg);
    }


    public interface HandlerTemperatorUtilCallback {
        void tempCallback(int maxValue, int avgValue, int minValue, List<Integer> data);
    }


}
